﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 档案大类
    /// </summary>
    public class T_ArchiveTypeDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加档案大类型
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_ArchiveType(T_ArchiveType en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveType.Select(c => c).Where(c => c.ATYPE_NAME == en.ATYPE_NAME).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_ArchiveType.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 修改档案类型
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_ArchiveType(int id, string name)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveType.Select(c => c).Where(c => c.ATYPE_NAME ==name).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_ArchiveType.Select(c => c).Where(c => c.ATYPE_NAME == name);
                    foreach (var n in query)
                        n.ATYPE_NAME = name;
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 获取档案大类
        /// </summary>
        /// <returns></returns>
        public static List<T_ArchiveType> Get_ArchiveType()
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_ArchiveType.Select(c => c).OrderBy(c => c.ATYPE_ORDER).ThenBy(c => c.ATYPE_ID).ToList();
            }
            catch { return null; }
        }
        public static T_ArchiveType Get_ArchiveType(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_ArchiveType.Select(c => c).Where(c => c.ATYPE_ID == id).Single();
            }
            catch { return null; }
        }
    }
}
